using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonsPickLiquidManager : MonoBehaviour
{
	private System.Random rnd = new System.Random();

	private List<ButtonPickLiquidController> buttons = new List<ButtonPickLiquidController>();

	private float buttons_start_position_x = -135f;

	private float buttons_end_position_x = 15f;

	private float buttons_start_position_y = 400f;

	private float buttons_next_button_position_y = 120f;

	private float timefirstButtonSlideIn;

	private float timeBetweenButtonsSlideIn = 0.25f;

	private GameObject prefab;

	private LiquidPicker liquidPicker;

	private BottlesManager bottlesManager;

	private Coroutine liquidPickDelay;

	public void Awake()
	{
		prefab = Resources.Load<GameObject>("Prefabs/Button_PickLiquid");
		liquidPicker = UnityEngine.Object.FindObjectOfType<LiquidPicker>();
		bottlesManager = UnityEngine.Object.FindObjectOfType<BottlesManager>();
	}

	public void DeleteButton()
	{
		for (int i = 0; i < buttons.Count; i++)
		{
			if (buttons[i] != null)
			{
				buttons[i].SlideOutAnimation();
			}
		}
		buttons.Clear();
	}

	public void CreateButtons(List<LiquidType> types, GameObject canvasObject)
	{
		DeleteButton();
		List<LiquidType> list = new List<LiquidType>(types);
		Shuffle(list);
		CanvasWorldSpaceResize component = canvasObject.GetComponent<CanvasWorldSpaceResize>();
		Canvas component2 = canvasObject.GetComponent<Canvas>();
		RectTransform component3 = canvasObject.GetComponent<RectTransform>();
		float num = 0f - component3.rect.height + buttons_start_position_y;
		float num2 = buttons_end_position_x;
		for (int i = 0; i < list.Count; i++)
		{
			GameObject gameObject = component.CreateGameObjectAndSetPosition(prefab, new Vector3(num2 + buttons_start_position_x, num), prefab.name + "_" + list[i].ToString());
			ButtonPickLiquidController controller = gameObject.GetComponent<ButtonPickLiquidController>();
			controller.SetStartAndEndPosition(new Vector3(num2 + buttons_start_position_x, num), new Vector3(num2, num));
			controller.SetLiquidType(list[i]);
			controller.SetOnClickAction(delegate
			{
				OnClickButton(controller);
			});
			buttons.Add(controller);
			num += buttons_next_button_position_y;
		}
		StartCoroutine(PlaySlideInAnimation(buttons, reverse: false));
	}

	public void Shuffle<T>(IList<T> list)
	{
		int num = list.Count;
		while (num > 1)
		{
			num--;
			int index = rnd.Next(num + 1);
			T value = list[index];
			list[index] = list[num];
			list[num] = value;
		}
	}

	private IEnumerator PlaySlideInAnimation(List<ButtonPickLiquidController> buttons, bool reverse)
	{
		yield return new WaitForSeconds(timefirstButtonSlideIn);
		if (!reverse)
		{
			for (int j = 0; j < buttons.Count; j++)
			{
				buttons[j].SlideInAnimation();
				yield return new WaitForSeconds(timeBetweenButtonsSlideIn);
			}
			yield break;
		}
		for (int i = buttons.Count - 1; i >= 0; i--)
		{
			buttons[i].SlideInAnimation();
			yield return new WaitForSeconds(timeBetweenButtonsSlideIn);
		}
	}

	private void OnClickButton(ButtonPickLiquidController controller)
	{
		SelectLiquid(controller.liquidType);
	}

	public void SelectLiquid(LiquidType liquidType)
	{
		string liquidTypeString = liquidType.ToString();
		bottlesManager.ShowBottle(liquidType);
		if (liquidPickDelay != null)
		{
			StopCoroutine(liquidPickDelay);
		}
		liquidPickDelay = StartCoroutine(LiquidPickDelay(liquidTypeString));
		for (int i = 0; i < buttons.Count; i++)
		{
			if (liquidType == buttons[i].liquidType)
			{
				buttons[i].ZoomIn();
			}
			else
			{
				buttons[i].ZoomOut();
			}
		}
	}

	private IEnumerator LiquidPickDelay(string liquidTypeString)
	{
		int previousCount = 0;
		int activeLiquids2 = 0;
		LiquidFallingController[] controllers2 = UnityEngine.Object.FindObjectsOfType<LiquidFallingController>();
		LiquidFallingController[] array = controllers2;
		foreach (LiquidFallingController liquidFallingController in array)
		{
			if (!liquidFallingController.zeroLiquidValue)
			{
				activeLiquids2++;
			}
		}
		if (activeLiquids2 > 0)
		{
			while (true)
			{
				activeLiquids2 = 0;
				controllers2 = UnityEngine.Object.FindObjectsOfType<LiquidFallingController>();
				LiquidFallingController[] array2 = controllers2;
				foreach (LiquidFallingController liquidFallingController2 in array2)
				{
					if (!liquidFallingController2.zeroLiquidValue)
					{
						activeLiquids2++;
					}
				}
				if (activeLiquids2 < previousCount)
				{
					break;
				}
				previousCount = activeLiquids2;
				yield return new WaitForEndOfFrame();
			}
		}
		liquidPicker.LiquidPicked(liquidTypeString);
		liquidPickDelay = null;
	}

	public void ZoomOutAllButtons()
	{
		for (int i = 0; i < buttons.Count; i++)
		{
			buttons[i].ZoomOut();
		}
	}
}
